package com.meadvc.ssmdemo.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sun.javaws.IconUtil;
import netscape.javascript.JSObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: ssm-demo
 * @description:
 * @author: meadvc
 * @create: 2020-06-19 00:08
 **/
public class BT_Algorithm {
    private  static BigDecimal total_price ;
    private  static BigDecimal best_price;

    private static List<JSONObject> templist = new ArrayList<>();
    private static List<JSONObject> bestlist = new ArrayList<>();

    public static JSONObject getBest(List<JSONObject> list){

        //初始化
        total_price = BigDecimal.valueOf(0);
        best_price = BigDecimal.valueOf(999999999);
        bestlist.clear();
        templist.clear();

        //调用回溯函数
        backtrack(list,0);

        //封装json返回数据
        JSONObject resultJson = new JSONObject();

        if(bestlist==null||bestlist.isEmpty()){
            resultJson = null;
        }else{
            resultJson.put("bestlist",bestlist);
            resultJson.put("best_price",best_price);
        }

        //System.out.println(resultJson);
        return resultJson;
    }

    public static void backtrack(List<JSONObject> list ,int index){
        if(index == list.size()){
            //终止条件处理
            if(total_price.compareTo(best_price)<0){
                best_price = total_price;
                bestlist.clear();
                bestlist.addAll(templist);
            }
        } else {
            //得到缓存列表中已有的供应商名称列表
            List<String> tems = new ArrayList<>();
            for(JSONObject c:templist){
                tems.add((String) c.get("sname"));
            }
            //处理当前种类的零件
            JSONObject jsonObject = list.get(index);
            String cname = (String) jsonObject.get("cname");//当前零件种类
            int num = (int)jsonObject.get("num");//当前零件数量
            List<JSONObject> li = (List<JSONObject>)jsonObject.get("data");//当前种类的所有零件
            for(int i = 0;i<li.size();i++){
                JSONObject json = li.get(i);
                if(tems.contains((String) (json.get("sname")))) continue;
                //如果供应商名称列表中没有该零件的供应商,则进行处理该零件
                //零件数据处理
                json.put("num",num);
                BigDecimal price = (BigDecimal)json.get("price");
                price = price.multiply(BigDecimal.valueOf(num));
                json.put("t_price",price);
                //总价格处理
                total_price = total_price.add(price);
                //将该零件添加到templist中
                templist.add(json);
                //System.out.println("--start--"+templist+"--"+total_price);
                //递归调用迭代算法，进入下一层
                backtrack(list,index+1);
                //上层递归结束处理
                templist.remove(templist.size()-1);
                total_price = total_price.subtract(price);
                //System.out.println("--end--"+templist+"--"+total_price);

            }
        }
    }
}
